/**    
 * @Title: HttpReader.java  
 * @Package cn.com.ytst.data.integration.components.reader.http  
 * @Description: TODO(用一句话描述该文件做什么)  
 * @author yanhewei@vv.cc.com    
 * @date 2020年7月5日 下午9:25:12  
 * @version V1.0    
*/
package cn.com.ytst.data.integration.components.reader.http;

import java.io.File;
import java.util.Map;

import com.alibaba.datax.common.util.Configuration;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.ytst.framework.common.StringTools;
import com.ytst.framework.common.tools.JsonUtil;

import cn.com.ytst.data.integration.core.plugin.Reader;
import cn.com.ytst.data.integration.util.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

/**  
 * @ClassName: HttpReader  
 * @Description: http 调用 
 * @author yanhewei@vv.cc.com  
 * @date 2020年7月5日 下午9:25:12  
 *    
*/
@Slf4j
public class HttpReader extends Reader {

	String uri;
	String formframte;
	String parameters;
	String method;
	@Override
	public void init() {
		Configuration configuration = this.getPluginJobConf();
		this.uri = configuration.getString(HttpReaderKey.HTTP_READER_URI_TAG);
		this.formframte = configuration.getString(HttpReaderKey.HTTP_READER_FORMFRAMTE_TAG);
		this.method = configuration.getString(HttpReaderKey.HTTP_READER_METHOD_TAG);
		this.parameters = configuration.getString(HttpReaderKey.HTTP_READER_PARAMETER_TAG);	
	}

	@Override
	public void startReader() {
		switch (formframte) {
		case "application/x-www-form-urlencoded":
			if("GET".equals(this.method)) {
				try {
					
					String line = HttpUtil.get(this.uri);
					this.getPipleChannel().doPush(line);
				} catch (Exception e) {
					log.error("",e);
				}	
			}else {
				if(StringTools.isEquals("[]", this.parameters)) {
					String line = HttpUtil.post(this.uri);
					this.getPipleChannel().doPush(line);
				}else {
					Map<String,String> prarms = this.parseParameters();
					String line = HttpUtil.postEntity(this.uri,prarms);
					this.getPipleChannel().doPush(line);
				}
			}
			
			break;
		case "application/json":
			
			if("GET".equals(this.method)) {
				
			}else {
				
				Map<String,String> prarms = this.parseParameters();
				String line = HttpUtil.postMethodJsonEntity(this.uri,JsonUtil.objectToJson(prarms),null);
				this.getPipleChannel().doPush(line);
	           			
			}
			break;

		default:
			break;
		}
		
	}
	
	private Map<String,String> parseParameters(){
		JSONArray arrays = JSONArray.parseArray(parameters);
		Map<String,String> mapvalues = Maps.newHashMapWithExpectedSize(arrays.size());
		for(Object jsonObject : arrays) {
			Map<String,String> values =  JSONObject.parseObject(jsonObject.toString(),Map.class);
			mapvalues.putAll(values);
		}
		return mapvalues;
	}

	@Override
	public void destroy() {
		
		
	}
	
	public static void main(String[] args) {
		Configuration configuration = Configuration.from(new File("D:\\job\\http\\http_adsb.json"));
		Configuration conf = configuration.getConfiguration("job.content[0].reader");
		String p = conf.getString(HttpReaderKey.HTTP_READER_PARAMETER_TAG);
		JSONArray arrays = JSONArray.parseArray(p);
		Map<String,String> mapvalues = Maps.newHashMapWithExpectedSize(arrays.size());
		for(Object jsonObject : arrays) {
			Map<String,String> values =  JSONObject.parseObject(jsonObject.toString(),Map.class);
			mapvalues.putAll(values);
		}
		System.out.println(mapvalues.toString());
	}

}
